
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.cctv.esb._interface.ldapnotifysubscribeservice._1_0;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;

import com.cctv.eip.schema.ldap._1.DataType;
import com.cctv.eip.schema.ldap._1.Operation;
import com.cctv.esb.schema.pubsub._1.ResponseType;
import com.cctv.ldap.LdapDept;
import com.cctv.ldap.LdapPerson;
import com.cctv.ldap.LdapUtils;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.sms.platform.domain.Dept;
import com.ctvit.sms.platform.domain.Person;
import com.ctvit.sms.platform.service.SmsService;

/**
 * This class was generated by Apache CXF 2.5.0
 * 2012-12-24T10:18:38.769+08:00
 * Generated source version: 2.5.0
 * 
 */

@javax.jws.WebService(
                      serviceName = "LdapNotifySubscribeService",
                      portName = "LdapNotifySubscribeServiceHttpPort",
                      targetNamespace = "http://cctv.com/IMS/service/LdapNotifySubscribeService/1.0",
                      endpointInterface = "com.cctv.esb._interface.ldapnotifysubscribeservice._1_0.LdapNotifySub")
                      
public class LdapNotifySubImpl implements LdapNotifySub {

    private static final Logger LOG = Logger.getLogger(LdapNotifySubImpl.class.getName());

    /* (non-Javadoc)
     * @see com.cctv.esb._interface.ldapnotifysubscribeservice._1_0.LdapNotifySub#getServiceInfo(com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType  getServiceInfoRequest )*
     */
    public com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType getServiceInfo(com.cctv.esb.schema.monitor._0.GetServiceInfoRequestType getServiceInfoRequest) { 
        LOG.info("Executing operation getServiceInfo");
        System.out.println(getServiceInfoRequest);
        try {
            com.cctv.esb.schema.monitor._0.GetServiceInfoResponseType _return = null;
            return _return;
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }

    /* (non-Javadoc)
     * @see com.cctv.esb._interface.ldapnotifysubscribeservice._1_0.LdapNotifySub#subscribe(com.cctv.esb.schema.messagehead._0.RequestHeadType  requestHead ,)com.cctv.eip.schema.ldap._1.Notify  notify )*
     */
    public com.cctv.esb.schema.pubsub._1.ResponseType subscribe(com.cctv.esb.schema.messagehead._0.RequestHeadType requestHead,com.cctv.eip.schema.ldap._1.Notify notify) { 
    	String rt="0";
		try{
			if (DataType.USER.equals(notify.getType())){
	    		LdapUtils ldapUtils=new LdapUtils();
	    		String uid=notify.getDn();int i=0;int j=0;
	    		i=uid.indexOf("=");
	    		j=uid.indexOf(",");
	    		uid=uid.substring(i+1,j);
	    		LdapPerson ldapPerson= ldapUtils.getPersonByUID(uid);
	    		Person person=new Person();
	    		person.setZType("0");
	    		person.setZPerno(ldapPerson.getUid());
	    		person.setZPerna(ldapPerson.getCn());
	    		person.setZMobph(ldapPerson.getMobile());
	    		String  dept=ldapPerson.getOu();//ou=10000796,ou=10000021,cn=dept,dc=cctv,dc=com
	    		
	    		//处理部门代码，分离出中心代码，部门代码，科组代码
	    		System.out.println("返回的部门代码===="+dept);
	    		if(dept!=null&&!dept.equals("")){
	    			String[] deptList = dept.split(",");
	    			int js = 0;
	    			for(int k=0;k<deptList.length;k++){
	    				String str = deptList[k];
	    				if(str.indexOf("ou=")!=-1){
	    					js++;
	    				}
	    			}
	    			//只有中心代码
	    			if(js==1){
	    	    		i=deptList[0].indexOf("=");
	    	    		j=deptList[0].length();
	    	    		String zCentre=deptList[0].substring(i+1,j);
	    	    		person.setZCentre(zCentre);//中心代码
	    			}
	    			//有部门和中心代码
	    			if(js==2){
	    	    		i=deptList[0].indexOf("=");
	    	    		j=deptList[0].length();
	    	    		String zSector=deptList[0].substring(i+1,j);
	    	    		person.setZSector(zSector);//部门代码
	    	    		
	    	    		i=deptList[1].indexOf("=");
	    	    		j=deptList[1].length();
	    	    		String zCentre=deptList[1].substring(i+1,j);
	    	    		person.setZCentre(zCentre);//中心代码
	    			}
	    			//有部门和中心代码,科组代码
	    			if(js==3){
	    	    		i=deptList[0].indexOf("=");
	    	    		j=deptList[0].length();
	    	    		String zDept=deptList[0].substring(i+1,j);
	    	    		person.setZDept(zDept);//科组代码
	    				
	    	    		i=deptList[1].indexOf("=");
	    	    		j=deptList[1].length();
	    	    		String zSector=deptList[1].substring(i+1,j);
	    	    		person.setZSector(zSector);//部门代码
	    	    		
	    	    		i=deptList[2].indexOf("=");
	    	    		j=deptList[2].length();
	    	    		String zCentre=deptList[2].substring(i+1,j);
	    	    		person.setZCentre(zCentre);//中心代码
	    			}
	    		}
	    		System.out.println("科组代码==："+person.getZDept());
	    		System.out.println("部门代码==："+person.getZSector());
	    		System.out.println("中心代码==："+person.getZCentre());
	    		
	    		System.out.println("类型==："+notify.getOperation());
	    		
	    		SmsService smsService=(SmsService)ContextHolder.getBean(SmsService.class);
	    		if (Operation.C.equals(notify.getOperation())){
	    			System.out.println("增加==："+notify.getOperation());
	    			person.setZCrdat(new Timestamp(Calendar.getInstance().getTimeInMillis()));
	    			smsService.removePerson(person);
	    			smsService.insertPerson(person);
	    		}else if (Operation.D.equals(notify.getOperation())){
	    			System.out.println("删除==："+notify.getOperation());
	    			smsService.removePerson(person);
	    		}else if (Operation.U.equals(notify.getOperation())){
	    			System.out.println("修改==："+notify.getOperation());
	    			person.setZUpdat(new Timestamp(Calendar.getInstance().getTimeInMillis()));
	    			smsService.updatePerson(person);
	    		}
	    	}else if (DataType.OU.equals(notify.getType())){
	    		LdapUtils ldapUtils=new LdapUtils();
	    		String dn=notify.getDn().toLowerCase().replace(",cn=dept,dc=cctv,dc=com", "");
	    		LdapDept ldapDept= ldapUtils.getDeptByUID(dn);
	    		Dept dept=new Dept();
	    		dept.setZDeptId(ldapDept.getOu());
				dept.setZDeptNm(ldapDept.getDescription());
				dept.setZLevel(ldapDept.getPostalAddress());
				dept.setZOrg(ldapDept.getStreet());
				dept.setZParentId(ldapDept.getPostOfficeBox());
	    		SmsService smsService=(SmsService)ContextHolder.getBean(SmsService.class);
	    		if (Operation.C.equals(notify.getOperation())){
	    			dept.setZCrdat(new Timestamp(Calendar.getInstance().getTimeInMillis()));
	    			smsService.removeDept(dept);
	    			smsService.insertDept(dept);
	    		}else if (Operation.D.equals(notify.getOperation())){
	    			smsService.removeDept(dept);
	    		}else if (Operation.U.equals(notify.getOperation())){
	    			dept.setZUpdat(new Timestamp(Calendar.getInstance().getTimeInMillis()));
	    			smsService.updateDept(dept);
	    		}
	    	}
		}catch (Exception e) {
			e.printStackTrace();
			rt="1";
		}
    	if (("0").equals(rt)){
    		return ResponseType.OK;
    	}else {
    		return ResponseType.FAIL;
    	}
    }

}
